package com.bjwl.web.contrlller.branchstore;

import com.bjwl.pojo.*;
import com.bjwl.pojo.dto.CardStriPBrDto;
import com.bjwl.service.BranchStoreService;
import com.bjwl.service.CardStripService;
import com.bjwl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author: czc
 * @Date:Create：in 2020/5/11 19:15
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */
@Controller
@RequestMapping("/cardstrip")
public class CardStripController {

    @Autowired
    private CardStripService cardStripService;
    @Autowired
    private BranchStoreService branchStoreService;

    //跳转家政卡档案页面（并回显家政卡的使用记录）
    @RequestMapping("/jumpCardStriplist")
    public String jumpCardStriplist(HttpSession httpSession){
        List<CardStrip > cardStripListss = cardStripService.findAllCardStrip();
        httpSession.setAttribute("cardStripListss",cardStripListss);
        return "branch_oa/card-strip-list";
    }


    //********************************
    @RequestMapping("/findCardStripBybsid")
    public String findCardStripBybsid(HttpSession httpSession, HttpServletRequest request,Integer bsid, Integer pageNum, Integer cardstripid , String cardstripvalue , String wstarttime , String wendtime) {
        if(pageNum == null){
            pageNum =1;
        }

        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        bsid = bsadmin1.getBsid();
        PageBean<CardStrip> cardStripList = cardStripService.findCardStripBybsid(pageNum,bsid,cardstripid, cardstripvalue,wstarttime,wendtime);
        request.setAttribute("cardStripList", cardStripList);
        CardStriPBrDto cardStriPBrDto = new CardStriPBrDto(cardstripid,cardstripvalue,wstarttime,wendtime);
        request.setAttribute("cardStriPBrDto",cardStriPBrDto);
        return "branch_oa/card-strip-list";

    }
    //********************************


    //跳转cardstrip的修改页面
    @RequestMapping("/jumpCardStripEdit")
    public String jumpCardStripEdit(HttpSession httpSession , Integer id){
        CardStrip cardStrip = cardStripService.jumpCardStripEdit(id);
        httpSession.setAttribute("cardStrip",cardStrip);

        return "branch_oa/card-strip-edit";
    }


    //根据oid删除当前派单信息
    @RequestMapping("/deleteCardStrip")
    public @ResponseBody Boolean deleteCardStrip(Integer id){

        cardStripService.deleteCardStrip(id);
        return true;

    }

    @RequestMapping("/updateCardStripByid")
    public @ResponseBody Boolean updateCardStripByid(HttpSession httpSession ,CardStrip cardStrip){
        Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
        BranchStore branchStore = branchStoreService.findbStoreById(bsadmin.getBsid());
        String password = branchStore.getContacts();
        String pass = cardStrip.getFit();
        if(pass.equals(password)){
            cardStripService.updateCardStripByid(cardStrip);
            return true;
        }else {
            return false;
        }

    }

    @InitBinder
    public void initDateFormate(WebDataBinder dataBinder) {
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
}
